package com.chatsystem.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

/**
 * 消息实体类
 * 
 * @author ChatSystem
 * @version 1.0.0
 */
@Entity
@Table(name = "messages")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Message {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "消息内容不能为空")
    @Column(columnDefinition = "TEXT")
    private String content;

    @NotNull(message = "发送者不能为空")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sender_id", nullable = false)
    private User sender;

    @NotNull(message = "接收者不能为空")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "receiver_id", nullable = false)
    private User receiver;

    @Enumerated(EnumType.STRING)
    @Column(name = "message_type")
    private MessageType messageType = MessageType.TEXT;

    @Column(name = "file_url")
    private String fileUrl;

    @Column(name = "file_name")
    private String fileName;

    @Column(name = "file_size")
    private Long fileSize;

    @Column(name = "is_read")
    private Boolean isRead = false;

    @Column(name = "is_recalled")
    private Boolean isRecalled = false;

    @CreationTimestamp
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;

    @Column(name = "recalled_at")
    private LocalDateTime recalledAt;

    /**
     * 消息类型枚举
     */
    public enum MessageType {
        TEXT,       // 文本消息
        FILE,       // 文件消息
        IMAGE,      // 图片消息
        VOICE,      // 语音消息
        VIDEO       // 视频消息
    }
} 